package com.jinly.ryder.common.utils;

import org.springframework.util.ObjectUtils;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;

public class DateUtil {

    private static DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");

    public static LocalDateTime toLocal(Long timestamp) {
        if (ObjectUtils.isEmpty(timestamp)) return null;
        return LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneId.systemDefault());
    }

    public static Long toTimestamp(LocalDateTime time) {
        if (ObjectUtils.isEmpty(time)) return null;
        return time.toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
    }

    public static LocalDateTime parse(String time) {
        if (ObjectUtils.isEmpty(time)) return null;
        return LocalDateTime.parse(time, formatter1);
    }

}
